unit UnitOpenSession;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TFormOpenSession = class(TForm)
    ListBox1: TListBox;
    Label1: TLabel;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ListBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    FFileName: string;
    hEsc: LongInt;
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
  public
    { Public declarations }
    procedure ClearList;
    procedure AddItem(const Value: string);
    procedure DelItem(const Index: LongInt);
    property FileName: string read FFileName;
  end;

var
  FormOpenSession: TFormOpenSession;

implementation

uses Main;

{$R *.dfm}

procedure TFormOpenSession.Button1Click(Sender: TObject);
begin
  if (ListBox1.ItemIndex > -1) then
  begin
    FFileName := ListBox1.Items[ListBox1.ItemIndex];
    ModalResult := mrOk;
  end;
end;

procedure TFormOpenSession.Button2Click(Sender: TObject);
begin
  FFileName := '';
  ModalResult := mrCancel;
end;

procedure TFormOpenSession.FormCreate(Sender: TObject);
begin
  ModalResult := mrCancel;
  FFileName := '';
  hEsc := GlobalAddAtom('hEsc_OpenSession');
  RegisterHotKey(Handle, hEsc, 0, VK_ESCAPE);
end;

procedure TFormOpenSession.FormDestroy(Sender: TObject);
begin
  UnregisterHotKey(Handle, hEsc);
end;

procedure TFormOpenSession.FormShow(Sender: TObject);
begin
  FFileName := '';
  ListBox1.SetFocus;
end;

procedure TFormOpenSession.ListBox1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) then
   Button1Click(Sender);
end;

procedure TFormOpenSession.WMHotKey(var Msg: TWMHotKey);
begin
  with Msg do
  begin
    if (HotKey = hEsc) then
     ModalResult := mrCancel;
  end;
end;

procedure TFormOpenSession.ClearList;
begin
  ListBox1.Items.Clear;
end;

procedure TFormOpenSession.AddItem(const Value: string);
begin
  ListBox1.Items.Add(Value);
end;

procedure TFormOpenSession.DelItem(const Index: Integer);
begin
  ListBox1.Items.Delete(Index);
end;

end.
